﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;
// 使用mysql数据库必须要的头文件
//using MySql.Data.MySqlClient;

namespace WindowsFormsApplication3
{
    public partial class FormLogin : Form
    {
        string extPath = null;
        Configuration config = null;
        public FormLogin()
        {
            InitializeComponent();
            this.KeyPress += new KeyPressEventHandler(frmLogin_KeyPress);
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            extPath = Application.ExecutablePath;
            config = ConfigurationManager.OpenExeConfiguration(extPath);
            if (config.AppSettings.Settings["user"] != null)
            {
                tbUser.Text = config.AppSettings.Settings["user"].Value;
            }
            //ConfigurationSettings.AppSettings["user"];
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master";
            conn.Open();
            //创建命令对象
            OleDbCommand cmd = new OleDbCommand();
            //指定命令对象
            cmd.Connection = conn;
            //指定SQL类型
            cmd.CommandType = CommandType.Text;   //可以省略
            //指定SQL
            //cmd.CommandText = "select * from useraccount where username = '" + tbUser.Text + "' and userpassword = '" + tbPwd.Text + "'";
            //string fmt = "select * from useraccount where username = '{0}' and userpassword = '{1}'";            
            //cmd.CommandText = string.Format(fmt, tbUser.Text, tbPwd.Text);
            string sql = "select * from useraccount where username = ? and userpassword = ?";
            cmd.CommandText = sql;

            OleDbParameter param = new OleDbParameter();
            param.Value = tbUser.Text;
            cmd.Parameters.Add(param);

            param = new OleDbParameter();
            param.Value = tbPwd.Text;
            cmd.Parameters.Add(param);

            //执行命令
            OleDbDataReader reader = cmd.ExecuteReader();            
            bool loginSucceeded = false;
            //while (reader.Read())
            //{
            //    if (reader["username"].Equals(tbUser.Text) && reader["userpassword"].Equals(tbPwd.Text))
            //    {
            //        if (cbSaveUser.Checked)
            //        {
            //            config.AppSettings.Settings.Remove("user");
            //            config.AppSettings.Settings.Add("user", tbUser.Text);
            //        }
            //        config.Save(ConfigurationSaveMode.Modified);
            //        loginSucceeded = true;
            //        break;
            //    }
            //}
            if (reader.Read())
            {
                if (cbSaveUser.Checked)
                {
                    config.AppSettings.Settings.Remove("user");
                    config.AppSettings.Settings.Add("user", tbUser.Text);
                }
                config.Save(ConfigurationSaveMode.Modified);
                loginSucceeded = true;
            }
            else
            {
                MessageBox.Show("恭喜你，登录失败，请与网管联系！");
            }

            //通用的结果集和记录遍历代码
            //while (true)
            //{
            //    while (reader.Read())
            //    {
            //        MessageBox.Show(reader[0].ToString());
            //        break;
            //    }
            //    if (!reader.NextResult())
            //        break;
            //}
            System.AppDomain.CurrentDomain.SetData("Succeed", loginSucceeded);
        }


        private void tbUser_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (tbUser.Text != null && tbUser.Text != "")
                {
                    tbPwd.Focus();
                }
            }
        }

        private void tbPwd_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.Focus();
                if (tbPwd.Text != null && tbPwd.Text != "")
                {
                    btnLogin.PerformClick();
                }
            }
        }

        void frmLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 27)
            {
                btnClose.PerformClick();
            }
        }


        //private void btnClose_Click(object sender, EventArgs e)
        //{
        //    Close();
        //}

        private void tbUserPass_TextChanged(object sender, EventArgs e)
        {
            btnLogin.Enabled =!string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text);
            System.AppDomain.CurrentDomain.SetData("username", tbUser);
            System.AppDomain.CurrentDomain.SetData("password", tbPwd.Text.ToLower()); 
        }



        //private void tbPwd_TextChanged(object sender, EventArgs e)
        //{
        //    if (!string.IsNullOrEmpty(tbUser.Text) && !string.IsNullOrEmpty(tbPwd.Text))
        //    {
        //        btnLogin.Enabled = true;
        //    }
        //    else
        //    {
        //        btnLogin.Enabled = false;
        //    }
        //}

        public string UserName
        {
            get
            {
                return tbUser.Text.ToLower();
            }
            set
            {
                tbUser.Text = value;
            }
        }

        public string Password
        {
            get
            {
                return tbPwd.Text.ToLower(); 
            }
            set
            {
                tbPwd.Text = value;                
            }
        }

        public bool IsUserSaving
        {
            get
            {
                return cbSaveUser.Checked;
            }
        }

        public void SetFocus()
        {
            tbUser.Focus();
        }

        public string GetUserName()
        {
            return tbUser.Text.ToLower();
        }
    }
}

